package com.pingtai.security.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.pingtai.user.dao.PubUsersDao;

//你就可以从数据库中读入用户的密码，角色信息，是否锁定，账号是否过期

public class MyUserDetailService implements UserDetailsService  {
	
	private PubUsersDao pubUsersDao;
	 	
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
			
			Collection<GrantedAuthority> auths=new ArrayList<GrantedAuthority>();
			try{
				auths = pubUsersDao.findRoleByUserName(username);
			
			
			
			String password=null;
			List l = pubUsersDao.findUserByname(username);
			if(l==null||l.size()==0) return null; 
			//取得用户的密码
			password=((com.pingtai.user.entity.User) l.get(0)).getUserPassword();
			
			//List<PubAuthoritiesResources> aaa=pubAuthoritiesResourcesDao.getAll();
			
	        User user = new User(username,
	        		password, true, true, true, true, auths);
	        
	        return user;
			}catch(Exception e){
				e.printStackTrace();
			}
	        return null;
	}

	public PubUsersDao getPubUsersDao() {
		return pubUsersDao;
	}

	public void setPubUsersDao(PubUsersDao pubUsersDao) {
		this.pubUsersDao = pubUsersDao;
	}

	
	
	
	
}
